package com.windystory.app.antdesign;

import android.os.Environment;
import android.text.TextUtils;
import android.widget.Toast;

import com.open.net.client.impl.tcp.nio.NioClient;
import com.open.net.client.structures.BaseClient;
import com.open.net.client.structures.BaseMessageProcessor;
import com.open.net.client.structures.IConnectListener;
import com.open.net.client.structures.message.Message;
import com.windystory.socketlibrary.TCPClient;
import com.windystory.wslibrary.base.activity.BaseActivity;
import com.windystory.wslibrary.ui.button.Button;
import com.windystory.wslibrary.ui.edittext.EditText;
import com.windystory.wslibrary.util.LogUtil;
import com.windystory.wsmedialibrary.util.BitmapUtils;

import java.io.File;
import java.util.LinkedList;

public class SocketDemoActivity extends BaseActivity {

    private Button link, dislink;
    private EditText ip;
    private EditText port;
    private EditText tip;
    private Button send;
    private EditText content;
    private TCPClient client;
    private NioClient nioClient;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_socket_demo;
    }

    @Override
    protected boolean setEnableSwipeBack() {
        return false;
    }

    @Override
    protected void initView() {
        content = (EditText) findViewById(R.id.content);
        send = (Button) findViewById(R.id.send);
        dislink = (Button) findViewById(R.id.dislink);
        tip = (EditText) findViewById(R.id.tip);
        link = (Button) findViewById(R.id.link);
        ip = (EditText) findViewById(R.id.ip);
        port = (EditText) findViewById(R.id.port);
    }

    @Override
    protected void initData() {
        client = TCPClient.getInstance();
        nioClient = client.nio_initClient(
                new IConnectListener() {
                    @Override
                    public void onConnectionSuccess() {
                        runOnUiThread(() -> Toast.makeText(SocketDemoActivity.this, "连接成功", Toast.LENGTH_SHORT).show());

                    }

                    @Override
                    public void onConnectionFailed() {
                        runOnUiThread(() -> Toast.makeText(SocketDemoActivity.this, "连接失败", Toast.LENGTH_SHORT).show());
                    }
                },
                new BaseMessageProcessor() {
                    @Override
                    public void onReceiveMessages(BaseClient mClient, LinkedList<Message> mQueen) {
                        for (int i = 0; i < mQueen.size(); i++) {
                            Message msg = mQueen.get(i);
                            final String s = new String(msg.data, msg.offset, msg.length);
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    // tip.getText().append(s).append("\r\n");
                                }
                            });
                        }
                    }
                }
        );


    }



    @Override
    protected void initListener() {
        link.setOnClickListener(view -> {
            if (TextUtils.isEmpty(ip.getText().toString())) {
                Toast.makeText(SocketDemoActivity.this, "ip不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(port.getText().toString())) {
                Toast.makeText(SocketDemoActivity.this, "port不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            client.nio_setIPAndPORT(nioClient, ip.getText().toString(), Integer.parseInt(port.getText().toString()));
            client.nio_connect(nioClient);
        });
        dislink.setOnClickListener(view -> client.nio_disconnect(nioClient));
        send.setOnClickListener(view -> {
//            if (TextUtils.isEmpty(content.getText().toString())) {
//                Toast.makeText(SocketDemoActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
//                return;
//            }
            File file = new File(Environment.getExternalStorageDirectory() + "/1.png");
            if (file.exists()) {
                String data = BitmapUtils.imageToBase64(file.getPath());
                LogUtil.e(data);
                client.nio_sendData(nioClient, data.getBytes());
            }


        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        client.nio_disconnect(nioClient);
    }
}
